package com.museum.guide.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.museum.guide.common.result.Result;
import com.museum.guide.dto.GuideAppointmentDTO;
import com.museum.guide.entity.GuideAppointment;
import com.museum.guide.service.GuideAppointmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 讲解预约控制器
 */
@Slf4j
@RestController
@RequestMapping("/guide/appointment")
public class GuideAppointmentController {

    @Autowired
    private GuideAppointmentService appointmentService;

    /**
     * 分页查询讲解预约列表
     *
     * @param page     页码
     * @param pageSize 每页条数
     * @param userId   用户ID
     * @param status   状态
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<IPage<GuideAppointment>> page(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Long guideId
    ) {
        return Result.success(appointmentService.getPage(page, pageSize, userId, status, guideId));
    }

    /**
     * 根据ID查询讲解预约
     *
     * @param id 预约ID
     * @return 预约信息
     */
    @GetMapping("/{id}")
    public Result<GuideAppointment> getById(@PathVariable Long id) {
        log.info("根据ID查询讲解预约, id: {}", id);
        return Result.success(appointmentService.getById(id));
    }

    /**
     * 新增讲解预约
     *
     * @param appointmentDTO 预约DTO
     * @return 结果
     */
    @PostMapping
    public Result<String> save(@RequestBody @Valid GuideAppointmentDTO appointmentDTO) {
        log.info("新增讲解预约: {}", appointmentDTO);
        return Result.success(appointmentService.save(appointmentDTO));
    }

    /**
     * 修改讲解预约状态（管理员操作）
     *
     * @param id     预约ID
     * @param status 状态
     * @return 结果
     */
    @PutMapping("/status/{id}")
    public Result<String> updateStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        log.info("修改讲解预约状态, id: {}, status: {}", id, status);
        return Result.success(appointmentService.updateStatus(id, status));
    }

    /**
     * 取消讲解预约（用户操作）
     *
     * @param id      预约ID
     * @param request HTTP请求
     * @return 结果
     */
    @PutMapping("/cancel/{id}")
    public Result<String> cancel(@PathVariable Long id, HttpServletRequest request) {
        log.info("取消讲解预约, id: {}", id);

        // 从会话中获取用户ID
        Long userId = (Long) request.getSession().getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        return Result.success(appointmentService.cancel(id, userId));
    }

    /**
     * 查询用户的讲解预约列表
     *
     * @return 预约列表
     */
    @GetMapping("/user")
    public Result<List<GuideAppointment>> listByUser(Long userId) {
        return Result.success(appointmentService.listByUserId(userId));
    }
}